package finalproject.view;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import finalproject.model.rooms.Direction;
import finalproject.model.rooms.Mapper;
import finalproject.model.rooms.Room;

/**
 * The map panel
 * Represents the map with coloured Panel
 * @author Buisson Kevin & Bighetti Nicolas
 *
 */
@SuppressWarnings("serial")
public class MapPanel extends JPanel {

	
	/**
	 * All the map Panel
	 */
	private JPanel[][] mapPanel;

	/**
	 * the constructor of the MapPanel
	 * @param mapper the current mapper of the game
	 */
	public MapPanel(Mapper mapper)
	{
		// Set layout
		//super(new GridLayout(mapper.getMapHeight(), mapper.getMapWidth(), 5, 5));
		super(new GridLayout(mapper.getMapHeight(), mapper.getMapWidth()));
		
		// Configure panel
		this.setBackground(Color.WHITE);
		this.setBorder(null);

		// Init map
		this.initMap(mapper);
		for (int i = 0; i < mapPanel.length; i++)
			for (int j = 0; j < mapPanel[i].length; j++)
				// Add rooms panel
				this.add(mapPanel[i][j] != null ? mapPanel[i][j] : createRoomPanel(null));
	}

	/**
	 * Initilize the map from a given mapper
	 * Create the different Panel, colour them, and add borders if needed
	 * @param mapper the mapper to build the map from
	 */
	private void initMap(Mapper mapper)
	{
		this.mapPanel = new JPanel[mapper.getMapHeight()][mapper.getMapWidth()];

		Room room = mapper.getStartingRoom();
		for (int x = 0; x < mapper.getMapWidth(); x++)
		{
			this.mapPanel[mapper.getMapHeight() / 2 - 1][x] = createRoomPanel(room.getExit(Direction.NORTH));
			this.mapPanel[mapper.getMapHeight() / 2][x] = createRoomPanel(room);
			this.mapPanel[mapper.getMapHeight() / 2 + 1][x] = createRoomPanel(room.getExit(Direction.SOUTH));

			if(this.mapPanel[mapper.getMapHeight() / 2-1][x] != null)
				this.mapPanel[mapper.getMapHeight() / 2-1][x].setBorder(BorderFactory.createMatteBorder(1, 1, 0, 1, Color.gray));
			else
				this.mapPanel[mapper.getMapHeight() / 2-1][x].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray));
			
			if(this.mapPanel[mapper.getMapHeight() / 2 +1][x] != null)
				this.mapPanel[mapper.getMapHeight() / 2 +1][x].setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0,Color.gray));
			else
				this.mapPanel[mapper.getMapHeight() / 2 +1][x].setBorder(BorderFactory.createMatteBorder(0, 0, 0, 0,Color.gray));

			// Next room
			room = room.getExit(Direction.EAST);
		}
	}

	/**
	 * Crate a room panel from a given roomm
	 * If room is <code>null</code>, the color will differ
	 * @param room the room represnted by the current panel
	 * @return a new Panel representing the room
	 */
	private JPanel createRoomPanel(Room room)
	{
		JPanel roomPanel = new JPanel();
		roomPanel.setBackground((room != null) ? Color.yellow : Color.gray);
		if(room != null)
			roomPanel.setToolTipText(room.getShortDescription());
		return roomPanel;
	}
	

}
